package TestDemo.example.controller;

import TestDemo.example.pojo.Emp;
import TestDemo.example.pojo.EmpQueryParam;
import TestDemo.example.pojo.Result;
import TestDemo.example.service.EmpService;
import TestDemo.example.service.impl.EmpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

@Slf4j
@RestController
@RequestMapping (value = "/emps")
public class EmpController {
    @Autowired
    EmpService empService;
    @Autowired
    private EmpServiceImpl empServiceImpl;

//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1") Integer page
//                    ,@RequestParam (defaultValue = "10") Integer pageSize) {
//        log.info("分页查询正在执行");
//        return Result.success(empService.page(page, pageSize));
//    }

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("带条件的分页查询正在执行{}" ,empQueryParam);
        return Result.success(empService.pageEmp(empQueryParam));
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工{}" ,emp);
        empService.add(emp);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam ArrayList<Integer> ids) {
        log.info("删除员工{}" ,ids);
        empService.deleteEmpById(ids);
        return Result.success();
    }

    @GetMapping ("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("查询员工{}" ,id);
        Emp empinfo=empService.getById(id);
        log.info("查询员工{}成功" ,empinfo);
        return Result.success(empinfo);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工{}" ,emp);
        empService.update(emp);
        return Result.success();
    }


}
